﻿// E-Book Copier
// Copyright (C) 2009 Chuan-Gen Fan
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
// Project: http://code.google.com/p/ebookcopier/
// Contact Author:
//    website: http://chuangen.name/
// e-mail/msn: chuangen@live.cn

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EbookCopier
{
    /// <summary>
    /// 选项 对话框窗体。
    /// </summary>
    public partial class OptionsForm : Form
    {
        /// <summary>
        /// 常量类。
        /// </summary>
        private class Consts
        {
            internal const string CompanyLink = "http://chuangen.name/";
        }
        Properties.Settings settings = Properties.Settings.Default;
        TextBox[] HotkeyTBs = null;
        /// <summary>
        /// 初始化类 <see cref="OptionsForm"/> 的新实例。
        /// </summary>
        public OptionsForm()
        {
            InitializeComponent();

            HotkeyTBs = new TextBox[]{
                txtHotkeySetRect,
                txtHotkeyCapture,
                txtHotkeyStart,
                txtHotkeyPause,
                txtHotkeyStop
            };

            foreach (TextBox tb in HotkeyTBs)
            {
                tb.KeyDown += new KeyEventHandler(Hotkey_KeyDown);
                tb.KeyPress += delegate(object sender, KeyPressEventArgs e) { e.Handled = true; };
            }

            linkCompany.LinkClicked += new LinkLabelLinkClickedEventHandler(linkCompany_LinkClicked);

            btnBrowse.Click += new EventHandler(btnBrowse_Click);

            cmbImageFormat.Items.AddRange(new object[]{
                "BMP",
                "JPG",
                "PNG",
            });
            cmbImageFormat.SelectedItem = "PNG";

            btnOK.Click += new EventHandler(btnOK_Click);

            labelRectBorderColor.Click += new EventHandler(labelRectBorderColor_Click);
        }

        void labelRectBorderColor_Click(object sender, EventArgs e)
        {
            ColorDialog cd = new ColorDialog();
            cd.Color = labelRectBorderColor.BackColor;
            if (cd.ShowDialog(this) == DialogResult.OK)
                labelRectBorderColor.BackColor = cd.Color;
        }
        void LoadSettings()
        {
            if (settings.ImageFormat != null)
                cmbImageFormat.SelectedItem = settings.ImageFormat;
            txtPath.Text = settings.RecordPath;

            SetTextBox(txtHotkeyCapture, settings.CaptureHotkey);
            SetTextBox(txtHotkeySetRect, settings.SetRectHotkey);
            SetTextBox(txtHotkeyStart, settings.HotkeyStart);
            SetTextBox(txtHotkeyPause, settings.HotkeyPause);
            SetTextBox(txtHotkeyStop, settings.HotkeyStop);

            numCaptureInterval.Value = settings.CaptureInterval;
            labelRectBorderColor.BackColor = settings.FocusBorderColor;
        }
        void SaveSettings()
        {
            settings.RecordPath = txtPath.Text;
            string format =cmbImageFormat.SelectedItem as string;
            settings.ImageFormat = (format == null || format.Trim() == "") ? "PNG" : format;

            TextBox tb = txtHotkeyCapture;
            if (tb.Tag != null && tb.Tag.GetType() == typeof(HotkeyValue))
                settings.CaptureHotkey = tb.Tag as HotkeyValue;
            tb = txtHotkeySetRect;
            if (tb.Tag != null && tb.Tag.GetType() == typeof(HotkeyValue))
                settings.SetRectHotkey = tb.Tag as HotkeyValue;
            tb = txtHotkeyStart;
            if (tb.Tag != null && tb.Tag.GetType() == typeof(HotkeyValue))
                settings.HotkeyStart = tb.Tag as HotkeyValue;
            tb = txtHotkeyPause;
            if (tb.Tag != null && tb.Tag.GetType() == typeof(HotkeyValue))
                settings.HotkeyPause = tb.Tag as HotkeyValue;
            tb = txtHotkeyStop;
            if (tb.Tag != null && tb.Tag.GetType() == typeof(HotkeyValue))
                settings.HotkeyStop = tb.Tag as HotkeyValue;

            settings.CaptureInterval = (int)numCaptureInterval.Value;
            settings.FocusBorderColor = labelRectBorderColor.BackColor;
            settings.Save();
        }
        void btnOK_Click(object sender, EventArgs e)
        {
            SaveSettings();
            this.DialogResult = DialogResult.OK;
        }
        /// <summary>
        /// 引发 <see cref="E:System.Windows.Forms.Form.Load"></see> 事件。
        /// </summary>
        /// <param name="e">包含事件数据的 <see cref="T:System.EventArgs"></see>。</param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            LoadSettings();
        }


        void btnBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.Description = "选择用于存储日志文件的目录。";
            fbd.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            fbd.ShowNewFolderButton = true;

            DialogResult result = fbd.ShowDialog(this.TopLevelControl);
            if (result == DialogResult.OK)
            {
                txtPath.Text = fbd.SelectedPath;
            }
        }

        private void linkCompany_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {//访问网站
            try
            {
                System.Diagnostics.Process.Start(Consts.CompanyLink);
            }
            catch
            {
                //无法打开网站。
            }
        }
        void SetTextBox(TextBox tb, HotkeyValue key)
        {
            tb.Text = key.ToString();
            tb.Select(tb.Text.Length, 0);
            tb.Tag = key;
        }

        void Hotkey_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == 0) //没有修改键
            {
                e.Handled = true;
                return;
            }

            HotkeyValue value = null;
            if (e.KeyCode == Keys.ShiftKey || e.KeyCode == Keys.ControlKey || e.KeyCode == Keys.Menu || e.KeyCode == Keys.None)
            {//除修改键外，没有按键
                value = new HotkeyValue(e.Modifiers, Keys.None);
            }
            else
            {
                value = new HotkeyValue(e.Modifiers, e.KeyCode);
            }

            SetTextBox(sender as TextBox, value);

            e.Handled = true;
            base.OnKeyDown(e);
        }

    }
}